/*
 *    Copyright (c) 2018-2025, zhulj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: zhulj (1147212910@qq.com)
 */
package com.cws4cloud.cws.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cws4cloud.cws.admin.api.entity.SysDept;
import com.cws4cloud.cws.admin.api.entity.SysRole;
import com.cws4cloud.cws.admin.api.entity.SysUser;
import com.cws4cloud.cws.admin.api.entity.SysUserRole;
import com.cws4cloud.cws.admin.entity.Tenant;
import com.cws4cloud.cws.admin.mapper.*;
import com.cws4cloud.cws.admin.service.TenantService;
import com.cws4cloud.cws.common.mybatis.tenant.TenantId;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 租户表
 *
 * @author zhulj
 * @date 2021-01-20 23:04:53
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements TenantService {

	private final TenantId tenantId;

	private final SysRoleMapper sysRoleMapper;

	private final SysDeptMapper sysDeptMapper;

	private final SysUserMapper sysUserMapper;

	private final SysUserRoleMapper userRoleMapper;

	private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

	@Override
	public boolean saveTenant(Tenant tenant) {
		String code = tenantId.generate();
		tenant.setTenantId(code);
		// 新建租户对应的默认角色
		SysRole role = new SysRole();
		role.setTenantId(code);
		role.setRoleName("管理员");
		role.setRoleCode("admin");
		role.setRoleDesc(tenant.getTenantName()+"的管理员角色");
		sysRoleMapper.insert(role);
		// 新建租户对应的默认部门
		SysDept dept = new SysDept();
		dept.setTenantId(code);
		dept.setParentId(0);
		dept.setName(tenant.getTenantName());
		dept.setSort(2);
		sysDeptMapper.insert(dept);
		// 新建租户对应的默认管理用户
		SysUser user = new SysUser();
		user.setTenantId(code);
		user.setUsername("admin");
		user.setPassword(ENCODER.encode("123456"));
		user.setDeptId(dept.getDeptId());
		user.setPhone(tenant.getContactNumber());
		boolean temp = super.save(tenant);
		sysUserMapper.insert(user);
		SysUserRole userRole = new SysUserRole(user.getUserId(), role.getRoleId());
		userRoleMapper.insert(userRole);
		return temp;
	}
}
